package com.quantumoctopus.magiceatball;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;

public class ResultsWorker {

	// Returns one fully built out result from a given JsonElement. You can get
	// a JsonElement from the JsonWorker methods.

	// The JsonElement is one business. This fills in every property of a Result
	// and returns it.

	// I know it looks like a damn mess, but it is like 25 lines of almost the
	// same thing
	public static Result buildResult(JsonElement jse) {
		Result r = new Result();

		if (jse.getAsJsonObject().get("is_claimed") != null)
			r.setIs_claimed(jse.getAsJsonObject().get("is_claimed")
					.getAsString());

		if (jse.getAsJsonObject().get("rating") != null)
			r.setRating(jse.getAsJsonObject().get("rating").getAsString());

		if (jse.getAsJsonObject().get("mobile_url") != null)
			r.setMobile_url(jse.getAsJsonObject().get("mobile_url")
					.getAsString());

		if (jse.getAsJsonObject().get("rating_img_url") != null)
			r.setRating_img_url(jse.getAsJsonObject().get("rating_img_url")
					.getAsString());

		if (jse.getAsJsonObject().get("review_count") != null)
			r.setReview_count(jse.getAsJsonObject().get("review_count")
					.getAsString());

		if (jse.getAsJsonObject().get("name") != null)
			r.setName(jse.getAsJsonObject().get("name").getAsString());

		if (jse.getAsJsonObject().get("snippet_image_url") != null)
			r.setSnippet_image_url(jse.getAsJsonObject()
					.get("snippet_image_url").getAsString());

		if (jse.getAsJsonObject().get("rating_img_url_small") != null)
			r.setRating_img_url_small(jse.getAsJsonObject()
					.get("rating_img_url_small").getAsString());

		if (jse.getAsJsonObject().get("url") != null)
			r.setUrl(jse.getAsJsonObject().get("url").getAsString());

		if (jse.getAsJsonObject().get("phone") != null)
			r.setPhone(jse.getAsJsonObject().get("phone").getAsString());

		if (jse.getAsJsonObject().get("snippet_text") != null)
			r.setSnippet_text(jse.getAsJsonObject().get("snippet_text")
					.getAsString());

		if (jse.getAsJsonObject().get("image_url") != null)
			r.setImage_url(jse.getAsJsonObject().get("image_url").getAsString());

		if (jse.getAsJsonObject().get("display_phone") != null)
			r.setDisplay_phone(jse.getAsJsonObject().get("display_phone")
					.getAsString());

		if (jse.getAsJsonObject().get("rating_img_url_large") != null)
			r.setRating_img_url_large(jse.getAsJsonObject()
					.get("rating_img_url_large").getAsString());

		if (jse.getAsJsonObject().get("id") != null)
			r.setId(jse.getAsJsonObject().get("id").getAsString());

		if (jse.getAsJsonObject().get("is_closed") != null)
			r.setIs_closed(jse.getAsJsonObject().get("is_closed").getAsString());

		if (jse.getAsJsonObject().getAsJsonObject("location").get("city") != null)
			r.setCity(jse.getAsJsonObject().getAsJsonObject("location")
					.get("city").getAsString());

		if (jse.getAsJsonObject().getAsJsonObject("location")
				.get("geo_accuracy") != null)
			r.setGeo_accuracy(jse.getAsJsonObject().getAsJsonObject("location")
					.get("geo_accuracy").getAsString());

		if (jse.getAsJsonObject().getAsJsonObject("location")
				.get("postal_code") != null)
			r.setPostal_code(jse.getAsJsonObject().getAsJsonObject("location")
					.get("postal_code").getAsString());

		if (jse.getAsJsonObject().getAsJsonObject("location")
				.get("country_code") != null)
			r.setCountry_code(jse.getAsJsonObject().getAsJsonObject("location")
					.get("country_code").getAsString());

		if (jse.getAsJsonObject().getAsJsonObject("location").get("coordinate")
				.getAsJsonObject().get("latitude") != null)
			r.setLatitude(jse.getAsJsonObject().getAsJsonObject("location")
					.get("coordinate").getAsJsonObject().get("latitude")
					.getAsString());

		if (jse.getAsJsonObject().getAsJsonObject("location").get("coordinate")
				.getAsJsonObject().get("longitude") != null)
			r.setLongitude(jse.getAsJsonObject().getAsJsonObject("location")
					.get("coordinate").getAsJsonObject().get("longitude")
					.getAsString());

		if (jse.getAsJsonObject().getAsJsonObject("location").get("state_code") != null)
			r.setState_code(jse.getAsJsonObject().getAsJsonObject("location")
					.get("state_code").getAsString());

		// Categories, Address and Display_Address are returned as arrays and
		// needed to be set differently
		JsonArray tempJsa;
		try {
			tempJsa = jse.getAsJsonObject().get("categories").getAsJsonArray();
			String[] categories = new String[tempJsa.size()];
			for (int i = 0; i < tempJsa.size(); i++) {
				categories[i] = tempJsa.get(i).toString();
			}
			r.setCategories(categories);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		try {
			tempJsa = jse.getAsJsonObject().getAsJsonObject("location")
					.get("display_address").getAsJsonArray();
			String[] display_address = new String[tempJsa.size()];
			for (int i = 0; i < tempJsa.size(); i++) {
				display_address[i] = tempJsa.get(i).toString();
			}
			r.setDisplay_address(display_address);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		try {
			tempJsa = jse.getAsJsonObject().getAsJsonObject("location")
					.get("address").getAsJsonArray();
			String[] address = new String[tempJsa.size()];
			for (int i = 0; i < tempJsa.size(); i++) {
				address[i] = tempJsa.get(i).toString();
			}
			r.setAddress(address);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return r;
	}

	// Given a SIGNED url, returns the number of results found

	// This can be useful to figure out if more questions are required, or of
	// course if there are no matches
	public static String getNumberOfResults(String signedURL) {
		String numResults = null;
		try {
			numResults = JSONWorker
					.getJSONElementFromString(
							JSONWorker.getJSONString(signedURL))
					.getAsJsonObject().get("total").toString();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return numResults;
	}

	// use this if you've already got the JsonElement
	public static String getNumberOfResults(JsonElement jse) {
		String numResults = jse.getAsJsonObject().get("total").toString();

		return numResults;
	}

	// This will return an array of businesses. I figured this would be common
	// operation

	// Note, yelps API only returns the first 20 businesses, no matter how many
	// many are actually, available. There are ways to get businesses past 20,
	// but it requires additional API hits.
	public static JsonArray getJsonArrayOfBusinesses(String signedURL) {
		JsonArray jsa = null;
		JsonElement json = null;
		try {
			json = JSONWorker.getJSONElementFromString(JSONWorker
					.getJSONString(signedURL));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (json != null) {
			jsa = json.getAsJsonObject().getAsJsonArray("businesses");
		}
		return jsa;
	}

	// use this if you've already got the JsonElement
	public static JsonArray getJsonArrayOfBusinesses(JsonElement jse) {

		JsonArray jsa = jse.getAsJsonObject().getAsJsonArray("businesses");

		return jsa;
	}

	public static Result getResultByID(String id) {
		Result r = null;
		StringBuilder sb = new StringBuilder();
		sb.append("http://api.yelp.com/v2/business/");
		sb.append(id);
		String URL = sb.toString();
		String signedURL = YelpURLSigner.getURLString(URL);
		JsonElement jse = null;
		try {
			jse = JSONWorker.getJSONElementFromString(JSONWorker
					.getJSONString(signedURL));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (jse != null) {
			r = buildResult(jse);
		}

		return r;
	}

	public static int getPrice(String url) {
		int numprice = 0;
		String text;
		try {
			text = JSONWorker.getJSONString(url);
			// Find price_tip tag then count chars between end of tag and new
			// tag(where the $ are located)
			int temp = text.indexOf('>', text.indexOf("price_tip"));
			int tempo = text.indexOf('<', temp);
			numprice = tempo - temp - 1;

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return numprice;

	}

}
